package IO_Flow;

import java.io.*;

public class Homework1 {
    public static void Write(String name,String site){
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(name,true);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        byte[] str = site.getBytes();
        try {
            fos.write(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void read(String site){
        FileInputStream fis = null;
        StringBuffer num = new StringBuffer();
        try {
            fis = new FileInputStream(site);
        } catch (Exception e) {
            e.printStackTrace();
        }

        byte[] str = new byte[1024];
        try {
            int len = fis.read(str,0,str.length);
            while (len> 0){
                String content = new String(str,0,len);
                num.append(content);
                System.out.println(num.toString());
                len=(fis.read(str,0,str.length));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Write("test.txt","hello world");
        read("test.txt");
    }
}
